//
//  BEEvaluateController.swift
//  BEGlobalOiler
//
//  Created by KKING on 16/10/7.
//  Copyright © 2016年 Bullseye Energy pte ltd. All rights reserved.
//

import UIKit
import SnapKit

class BEEvaluateController: UIViewController {
    
    var talentName: String = ""
    var ordertalentId: String = ""
    
    
    @IBOutlet weak var starView1: UIView!
    @IBOutlet weak var starView2: UIView!
    @IBOutlet weak var starView3: UIView!
    @IBOutlet weak var starView4: UIView!
    @IBOutlet weak var starView5: UIView!
    
    @IBOutlet weak var talentNameTF: UILabel!
    
    @IBAction func followBtnDidClick(sender: UIButton) {
        sender.isSelected = !sender.isSelected
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = COLOR_ALL_BG
        talentNameTF.text = talentName
        setupStartView()
        JFProgressHUD.showloading()
        loadAllGradeInfo { (success, tip) in
            if success {
                JFProgressHUD.showSuccessWithStatus(status: tip)
            } else {
                JFProgressHUD.showInfoWithStatus(status: tip)
            }
        }
    }
    
    private func setupStartView(){
        let lView1 = SNStarView()
        lView1.frame = starView1.bounds
        starView1.addSubview(lView1)

        let lView2 = SNStarView()
        lView2.frame = starView2.bounds
        starView2.addSubview(lView2)
        
        let lView3 = SNStarView()
        lView3.frame = starView3.bounds
        starView3.addSubview(lView3)
        
        let lView4 = SNStarView()
        lView4.frame = starView4.bounds
        starView4.addSubview(lView4)
        
        let lView5 = SNStarView()
        lView5.frame = starView5.bounds
        starView5.addSubview(lView5)
    }
    
    
    func loadAllGradeInfo(finished: @escaping (_ success: Bool, _ tip: String) -> ()) {
        BENetworkTools.shareNetworkTool.loadAllGradeInfo { (success, result, error) in
            guard let resultjson = result else {
                finished(false, "Network fault!")
                return
            }
            if resultjson["success"] == true {
                //此处增加字典转模型逻辑
//                guard let arr = resultjson["result"].arrayObject else{
//                    return
//                }
//                guard let dictArr = arr as? [[String: AnyObject]] else{
//                    return
//                }
//                DispatchQueue.main.async(execute: {
//                    
//                    
//                })
                finished(true, "Load data success!")
            } else {
                finished(false, resultjson["message"].stringValue)
            }
        }
    }
    
    
    
    @IBAction func submitEvaluate(sender: UIButton) {
        let parameters: [String : AnyObject] = ([
            "comment": "string" as AnyObject,
            "follow": false as AnyObject,
            "talentGradeVOList" : [
                [
                    "id": 0,
                    "orderTalentId": "\(ordertalentId)",
                    "gradeInfoId": 1,
                    "grade": 5,
                    ],[
                        "id": 0,
                        "orderTalentId": "\(ordertalentId)",
                        "gradeInfoId": 2,
                        "grade": 5,
                        ],
                      [
                        "id": 0,
                        "orderTalentId": "\(ordertalentId)",
                        "gradeInfoId": 3,
                        "grade": 5,
                        ],
                      [
                        "id": 0,
                        "orderTalentId": "\(ordertalentId)",
                        "gradeInfoId": 4,
                        "grade": 5,
                        ],
                      [
                        "id": 0,
                        "orderTalentId": "\(ordertalentId)",
                        "gradeInfoId": 5,
                        "grade": 5,
                        ]
            ]] as AnyObject) as! [String : AnyObject]

        JFProgressHUD.showWithStatus(status: "Evaluate submit...")
        BENetworkTools.shareNetworkTool.talentEvaluate(ordertalentId: ordertalentId, parameters: parameters) { (success, result, error) in
            
            guard let resultjson = result else {
                JFProgressHUD.showWithStatus(status: "Network fault!")
                return
            }
            if resultjson["success"] == true {
                JFProgressHUD.showSuccessWithStatus(status: "Evaluate success!")
            } else {
                JFProgressHUD.showInfoWithStatus(status: resultjson["message"].stringValue)
            }
        }
        
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: ChangeImgHidNotification), object: nil)
        _ = navigationController?.popViewController(animated: true)
    }
}
